package cn.hs.servlet;

import cn.hs.dao.GoodsDao;
import cn.hs.dao.impl.GoodsDaoImpl;
import cn.hs.entity.Goods;
import cn.hs.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/goods")
public class GoodsServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        GoodsDao goodsDao=new GoodsDaoImpl();
        if ("querySearchShopGoodsList".equals(action)){
            Map<String,String> map =new HashMap<>();
            String goodsName = req.getParameter("goodsName");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String goodsStatus = req.getParameter("goodsStatus");
            String shopName = req.getParameter("shopName");
            map.put("shopName", shopName);
            map.put("goodsName",goodsName);
            map.put("goodsStatus",goodsStatus);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            //连接数据库

            //动态获取社区集合信息
            List<Goods> list = goodsDao.selectGoods(map);
            int total = goodsDao.selectGoodsCount(map);

            //创建一个返回值的map集合,用来包裹list和total
            Map resultMap =new HashMap<>();
            resultMap.put("list",list);
            resultMap.put("total",total);
            //把结果集合,封装到result对象中
            Result success = Result.success(resultMap);
            resp.getWriter().write(JSON.toJSONString(success));
        } else if ("modifyShopGoods".equals(action)) {
            int row = goodsDao.update(req.getParameter("goodsId"),req.getParameter("goodsStatus"));
            if(row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
